package com.chalk.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xiaoqiang
 * @Description Common 工具类
 * @date 2018/10/31 10:53
 */
public class CommonUtil {

    /**
     * 随机六位数字验证码
     *
     * @return 验证码
     */
    public static String smsCode() {
        return String.valueOf(new Random().nextInt(899999) + 100000);
    }

    /**
     * 校验手机号
     *
     * @param phoneNumber 手机号
     * @return 通过:true,不通过:false
     */
    public static boolean isPhoneNumber(String phoneNumber) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phoneNumber.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phoneNumber);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

    /**
     * 判断集合是否为空(null or size is zero)
     *
     * @param collection 集合
     * @return
     */
    public static boolean isNotBlankOfCollection(Collection<?> collection) {
        return collection != null && collection.size() > 0 ? true : false;
    }

    /**
     * 获取物品属性值
     *
     * @param attribute     物品属性
     * @param attributeName 物品属性名
     * @return
     */
    public static String getGoodsAttributeValue(String attribute, String attributeName) {
        if (StringUtils.isNotBlank(attribute) && StringUtils.isNotBlank(attributeName)) {
            String[] attributes = attribute.split(",");
            for (String string : attributes) {
                String[] strings = string.split(":");
                if (strings[0].equals(attributeName)) {
                    return strings[1];
                }
            }
        }
        return null;
    }
}
